import util from '../../util/index.js'
Page({
  pageData: {
    station: "",
  },

  /**
   * 页面的初始数据
   */
  data: {
    route: {
      stations: []
    },
    dates: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id;
    wx.showLoading({
      title: '操作中...',
    })

    util.ajax("route", {
      data: {
        action: 'detailAndDate',
        id
      }
    }).then(({
      code,
      data,
      msg
    }) => {
      wx.hideLoading()
      if (code == 1) {
        if (data.type == 1) {
          data.stations.pop();
        } else {
          data.stations.splice(0, 1);
        }
        if(data.dates.length == 0){
          util.tip("暂无可预约的日期");
        }
        data.dates.map(item=>{
          item.checked = true;
          item.result = "";
          item.className = "checked";
        })
        this.setData({
          route: data,
        })
        wx.setNavigationBarTitle({
          title: data.number,
        })
      } else {
        util.tip(msg);
      }
    }, wx.hideLoading)
  },

  radioChange({
    detail: {
      value
    }
  }) {
    this.pageData.station = value;
  },
  changeDate({
    currentTarget: {
      dataset: {
        index
      }
    }
  }) {
    let checked = this.data.route.dates[index].checked;
    this.setData({
      [`route.dates[${index}].checked`]: !checked,
      [`route.dates[${index}].className`]: checked ? '' : 'checked',
    })
  },
  book() {
    const {
      station
    } = this.pageData;
    if (station == "") {
      util.tip("请选择站点")
      return;
    }
    const {
      route
    } = this.data;
    const {dates} = route;
    if (dates.every(item => !item.checked)) {
      util.tip("请选择预约时间");
      return;
    }
    wx.showLoading({
      title: '操作中...',
    })
    const ds = [];
    dates.map(({
      year,
      date,
      checked
    }) => {
      if (checked) {
        ds.push(year + "-" + date)
      }
    })
    util.ajax("order", {
      data: {
        action: "bookByDates",
        dates: ds,
        name: station,
        rid: route._id,
      }
    }).then(({
      code,
      data,
      msg
    }) => {
      wx.hideLoading();
      if (code == 1) {
        let index = 0;
        //循环获取结果
        dates.map((item) => {
          if (!item.checked) {
            return;
          }
          const res = data[index];
          if (res) {
            const {
              code,
            } = res;
            //异常提醒颜色
            if (code != 1) {
              item.className = "error"
            }
          }
          item.result = data[index] ? data[index].msg : "";
          index++;
        });
        this.setData({
          route,
        })
        //根据类型不同，存储线路ID
        //预约成功后，该线路在首页第一条展示，便于再次预约（细节优化）
        let routeId = wx.getStorageSync("routeId");
        if (routeId) {
          routeId[route.type] = route._id;
        } else {
          routeId = {
            [route.type]: route._id
          };
        }
        wx.setStorageSync("routeId", routeId)
      } else {
        util.tip(msg)
      }
    }, wx.hideLoading)
  }

})